import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:get/get.dart';
import 'package:xm_app/common/ui/ui_adapter.dart';
import 'package:xm_app/model/HomeData.dart';
import 'package:xm_app/page/main/home/widget/HotTitleWidget.dart';
import 'package:xm_app/widget/CommonGrid.dart';
import 'package:xm_app/widget/CommonListTitle.dart';
import 'package:xm_app/widget/custom_refresh_widget.dart';

import 'TabsController.dart';

class RecommendTab extends StatefulWidget {
  @override
  _RecommendTabState createState() => _RecommendTabState();
}

class _RecommendTabState extends State<RecommendTab> {
  TabsController get controller => GetInstance().find<TabsController>();
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.grey.withOpacity(.1),
      child: CustomHeaderRefreshWidget(
        easyRefreshController: controller.easyRefreshController,
        onRefresh: () => controller.getRecommendData(),
        slivers: [
          HotWidget(),
          Obx(() => NormalWidget(
                title: "最新电影",
                iconData: Icons.movie_filter_outlined,
                list: controller.recommendData.value.m,
              )),
          Obx(() => NormalWidget(
                title: "最新动漫",
                iconData: Icons.movie_filter_outlined,
                list: controller.recommendData.value.c,
              )),
          Obx(() => NormalWidget(
                title: "最新连续剧",
                iconData: Icons.movie_filter_outlined,
                list: controller.recommendData.value.t,
              )),
          Obx(() => NormalWidget(
                title: "最新综艺",
                iconData: Icons.movie_filter_outlined,
                list: controller.recommendData.value.s,
              )),
          SliverToBoxAdapter(
            child: SizedBox(
              height: getHeight(10),
            ),
          )
        ],
      ),
    );
  }
}

class HotWidget extends GetView<TabsController> {
  @override
  Widget build(BuildContext context) {
    return Obx(() {
      return SliverToBoxAdapter(
        child: Padding(
          padding: EdgeInsets.only(right: 5, left: 5),
          child: Column(
            children: [
              HotTitleWidget(
                lastQty: controller.recommendData.value.lastQty,
                onRefreshPressed: () {
                  controller.refreshCurrentHotPageData();
                },
              ),
              CommonGrid(
                list: controller.getCurrentHotPageData(),
                isShowTag: true,
              )
            ],
          ),
        ),
      );
    });
  }
}

class NormalWidget extends StatelessWidget {
  final String title;
  final IconData iconData;
  final List<HomeDataItem> list;
  NormalWidget({this.title, this.iconData, this.list});
  @override
  Widget build(BuildContext context) {
    return SliverToBoxAdapter(
      child: Padding(
        padding: EdgeInsets.only(right: 5, left: 5),
        child: Column(
          children: [
            CommonListTitle(
              title: title,
              iconData: iconData,
            ),
            CommonGrid(
              list: list,
              isShowTag: true,
            )
          ],
        ),
      ),
    );
  }
}
